﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using BusinessObjects;
using DataAccess;
using DevExpress.XtraTreeList.Nodes;

namespace QuanLyHopDong
{
    public partial class BoPhanListView : Form
    {
        TreeListNode node;
        private List<BoPhan> listBoPhan;
        private BoPhan _boPhan;

        public BoPhanListView()
        {
            InitializeComponent();
        }

        private void BoPhanListView_Load(object sender, EventArgs e)
        {
            treelstBoPhan.OptionsView.ShowAutoFilterRow = false;
                       
            treelstBoPhan.OptionsBehavior.Editable = false;

            LoadData();
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            BoPhan boPhan =null;
            TreeListNode node = treelstBoPhan.FocusedNode;
            if (node != null)
                boPhan = node.Tag as BoPhan;
            _boPhan = new BoPhan();
            _boPhan.TrucThuoc = boPhan;
            OpenDetailForm();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
        private void treelstBoPhan_DoubleClick(object sender, EventArgs e)
        {
            node = treelstBoPhan.FocusedNode;
            _boPhan = node.Tag as BoPhan;
            if (node.ParentNode != null)
            {
                _boPhan.TrucThuoc = node.ParentNode.Tag as BoPhan;
            }
            OpenDetailForm();
        }

        private void OpenDetailForm()
        {
            BoPhanDetailView detailView = new BoPhanDetailView(_boPhan);
            detailView.DeleteChanged += new EventHandler(boPhanDetailView_DeleteChanged);
            detailView.SaveChanged += boPhanDetailView_SaveChanged;

            detailView.MdiParent = this.MdiParent;
            detailView.Show();
        }

        void boPhanDetailView_SaveChanged(object sender, SaveChangeEventArgs e)
        {
            if (e.IsNew)
            {
                LoadData();
            }
            else
            {
                if (node != null && _boPhan != null)
                {
                    node.SetValue(colMaQuanLy, _boPhan.MaQuanLy);
                    node.SetValue(colTenBoPhan, _boPhan.TenBoPhan);
                    node.Tag = _boPhan;
                    node.HasChildren = listBoPhan.Exists(b => b.TrucThuoc != null && b.TrucThuoc.Oid == _boPhan.Oid);
                }
            }
        }

        void boPhanDetailView_DeleteChanged(object sender, EventArgs e)
        {
            if (_boPhan != null)

                if (DataAccessObject.BoPhan.Delete(_boPhan) > 0)
                {
                    treelstBoPhan.Invalidate();
                }


        }

        private void LoadData()
        {
            listBoPhan = DataAccessObject.BoPhan.GetListBoPhan();
            treelstBoPhan.ClearNodes();
            LoadNode(null);
        }

        private void treeList1_BeforeExpand(object sender, DevExpress.XtraTreeList.BeforeExpandEventArgs e)
        {
            LoadNode(e.Node);
        }

        private void LoadNode(TreeListNode root)
        {
            
            //load nut cha
            if (root == null)
            {
                List<BoPhan> boPhanCha = GetListBoPhan(null);
                TreeListNode node;
                foreach (BoPhan boPhan in boPhanCha)
                {
                    node = treelstBoPhan.AppendNode(new object[] { boPhan.MaQuanLy, boPhan.TenBoPhan}, null);
                    node.Tag = boPhan;
                    node.HasChildren = listBoPhan.Exists(b => b.TrucThuoc != null && b.TrucThuoc.Oid == boPhan.Oid);
                }
            }
            //load nut con
            else if (root.Nodes.Count == 0)
            {
                BoPhan cha = root.Tag as BoPhan;
                if (cha != null)
                {
                    List<BoPhan> boPhanCon = GetListBoPhan(cha);
                    TreeListNode node;
                    foreach (BoPhan boPhan in boPhanCon)
                    {
                        node = treelstBoPhan.AppendNode(new object[] { boPhan.MaQuanLy, boPhan.TenBoPhan }, root);
                        node.Tag = boPhan;
                        node.HasChildren = listBoPhan.Exists(b => b.TrucThuoc != null && b.TrucThuoc.Oid == boPhan.Oid);
                    }
                }
            }
        }

        private List<BoPhan> GetListBoPhan(BoPhan boPhan)
        {
            List<BoPhan> result = new List<BoPhan>();
            if (boPhan != null)
                result = listBoPhan.FindAll(b => b.TrucThuoc != null && b.TrucThuoc.Oid == boPhan.Oid);
            else
                result = listBoPhan.FindAll(b => b.TrucThuoc == null);
            
            ////
            //result = listBoPhan.FindAll(delegate(BoPhan bp)
            //{
            //    return bp.Oid == boPhan.Oid;
            //});

            ////
            //foreach (BoPhan bp in listBoPhan)
            //{
            //    if (bp.Oid == boPhan.Oid)
            //        //xu ly here
            //}
            return result;
        }

        private void btnLamMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

    }
}
